package com.luhan.fmmall.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/webSocket/{oid}")
public class WebSocketServer {
    private static ConcurrentHashMap<String,Session> sessionMap = new ConcurrentHashMap<>();

    /**
     * 前端发送请求建立websocket连接，就会执行@OnOpen方法
     */
    @OnOpen
    public void open(@PathParam("oid")String orderId, Session session){
        sessionMap.put(orderId,session);
    }

    /**
     * 前端关闭页面或者主动关闭websocket连接，就会执行@OnOpen方法
     */
    @OnClose
    public void close(@PathParam("oid")String orderId, Session session){
        sessionMap.remove(orderId);
    }

    public static void sendMsg(String orderId,String msg){
        try {
            Session session = sessionMap.get(orderId);
            session.getBasicRemote().sendText(msg);//向前端发消息
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
